package org.wangchaocheng.cloudgallery.controller.user;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;

import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wangchaocheng.cloudgallery.api.user.UserServiceI;
import org.wangchaocheng.cloudgallery.assembler.ResponseAssembler;
import org.wangchaocheng.cloudgallery.commond.user.UserModifyCmd;
import org.wangchaocheng.cloudgallery.commond.user.UserRegisterCmd;
import org.wangchaocheng.cloudgallery.commond.user.UserRoleSaveCmd;
import org.wangchaocheng.cloudgallery.dto.common.PageDTO;
import org.wangchaocheng.cloudgallery.dto.user.UserDTO;
import org.wangchaocheng.cloudgallery.query.user.UserPageQry;

/**
 * 用户管理
 * @author wangchaocheng
 * @date 2025/03/28
 */
@RestController
@RequestMapping("user")
@RequiredArgsConstructor
public class UserController {

    private final UserServiceI userService;

    /**
     * 用户注册
     * @param cmd 注册参数
     * @return {@link SingleResponse}<{@link Boolean}> 注册结果
     */
    @PostMapping("register")
    public SingleResponse<Boolean> register(@Validated @RequestBody UserRegisterCmd cmd) {
        userService.register(cmd);
        return SingleResponse.of(true);
    }

    /**
     * 修改用户信息
     *
     * @param cmd 修改参数
     * @return {@link SingleResponse}<{@link Boolean}> 修改结果
     */
    @PutMapping("modify")
    public SingleResponse<Boolean> modify(@Validated @RequestBody UserModifyCmd cmd) {
        userService.modify(cmd);
        return SingleResponse.of(true);
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param userId 用户id
     * @return {@link SingleResponse}<{@link UserDTO}> 用户信息
     */
    @GetMapping("findById/{userId}")
    public SingleResponse<UserDTO> findById(@PathVariable("userId") Long userId) {
        UserDTO userDTO = userService.findById(userId);
        return SingleResponse.of(userDTO);
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param username 用户名
     * @return {@link SingleResponse}<{@link UserDTO}> 用户信息
     */
    @GetMapping("findByUsername/{username}")
    public SingleResponse<UserDTO> findByUsername(@PathVariable("username") String username) {
        UserDTO userDTO = userService.findByUsername(username);
        return SingleResponse.of(userDTO);
    }

    /**
     * 分页条件查询用户列表
     *
     * @param qry 查询条件
     * @return {@link PageResponse}<{@link UserDTO}> 用户信息
     */
    @GetMapping("listPage")
    public PageResponse<UserDTO> listPageUser(@RequestBody UserPageQry qry) {
        PageDTO<UserDTO> userPage = userService.listPageUser(qry);
        return ResponseAssembler.INSTANCE.toPageResponse(qry, userPage);
    }


    /**
     * 分配角色
     * @param cmd 参数
     * @return {@link SingleResponse}<{@link Boolean}>
     */
    @PostMapping("assignRole")
    public SingleResponse<Boolean> assignUserRoles(@Validated @RequestBody UserRoleSaveCmd cmd) {
        userService.assignUserRoles(cmd);
        return SingleResponse.of(true);
    }

}
